//
//  MainTabBarController.swift
//  WeiBo
//
//  Created by 刘星 on 16/6/4.
//  Copyright © 2016年 LiuXing. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        addChildVCs()
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        setupcomposeButton()
    }
    private func addChildVCs() {
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        let jsonData = NSData(contentsOfFile: path!)
        do {
            // 这个方法有个throws
            let dictArray = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers)
            for dict in dictArray as! [[String: String]] {
                addChildViewController(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
            }
        } catch {
            addChildViewControllers()
        }
    }
    private func addChildViewControllers() {
        addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
        addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
        addChildViewController("NullViewController", title: "", imageName: "")
        addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
        addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
    }
    private func addChildViewController(childControllerName: String, title: String, imageName: String) {
        // 命名空间
        let namespace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        // 告诉编译器暂时就是AnyClass
        let cls:AnyClass = NSClassFromString(namespace + "." + childControllerName)!
//        print(cls)
        // 告诉编译器真实类型是UIViewController
        let vcCls = cls as! UIViewController.Type
        // 实例化控制器
        let vc = vcCls.init()
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
//        tabBar.tintColor = UIColor.orangeColor()
        // 导航控制器
        let nav = UINavigationController()
        nav.addChildViewController(vc)
        addChildViewController(nav)
    }
    // 设置加号按钮
    private func setupcomposeButton () {
        tabBar.addSubview(composeButton)
        // 设置frame
        let width = UIScreen.mainScreen().bounds.size.width / CGFloat(viewControllers!.count)
        let rect  = CGRect(x: 0, y: 0, width: width, height: 49)
        // 第一个参数:是frame的大小
        // 第二个参数:是x方向偏移的大小
        // 第三个参数: 是y方向偏移的大小
        composeButton.frame = CGRectOffset(rect, 2 * width, 0)
    }
    // MARK: - ComposeButton
    private lazy var composeButton: UIButton = {
        let btn = UIButton()
        
        // 2.设置前景图片
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        // 3.设置背景图片
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 4.添加监听
        btn.addTarget(self, action: #selector(MainTabBarController.composeButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    // MARK: - ComposeButton点击
    func composeButtonClick() {
        print(#function)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
